import path from 'path';
import fs from 'fs';

const root = path.resolve(__dirname, '../');

function getRootPath() {
  return root;
}

export function getOutputDir(cwd?: string) {
  const root = cwd || process.cwd();
  return path.resolve(root, 'dist');
}

export function getDevServerOutputDir() {
  const root = process.cwd();
  return path.resolve(root, '_sites');
}

export function getHolderPath() {
  return path.resolve(root, 'src/data.ts');
}

export function getWxConfig(cwd = process.cwd()) {
  const file = path.resolve(cwd, 'wx.config.js');
  const defaultInjects = {
    server: (c: any) => c,
    webpack: (c: any) => c,
    port: 8020,
    injectScripts: [],
    injectStyles: [],
    modifyVars: {},
  };
  if (fs.existsSync(file)) {
    try {
      // eslint-disable-next-line @typescript-eslint/no-var-requires
      const config = require(file);
      return {
        ...defaultInjects,
        ...config,
        inject: {
          title: 'Wx API Docs',
          ...config.inject,
        },
      };
    } catch (e) {
      console.error(e);
    }
  }
  return {
    ...defaultInjects,
    inject: {
      title: 'Wx API Docs',
    },
  };
}
